<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="允许谁看" prop="sysUserId">
      <el-input v-model="dataForm.sysUserId" placeholder="允许谁看"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="内容"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="creatTime">
      <el-input v-model="dataForm.creatTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="已读" prop="reedId">
      <el-input v-model="dataForm.reedId" placeholder="已读"></el-input>
    </el-form-item>
    <el-form-item label="状态（0禁用1启用）" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态（0禁用1启用）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          sysUserId: '',
          title: '',
          content: '',
          creatTime: '',
          reedId: '',
          state: ''
        },
        dataRule: {
          sysUserId: [
            { required: true, message: '允许谁看不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          creatTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          reedId: [
            { required: true, message: '已读不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态（0禁用1启用）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelnoticeservice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sysUserId = data.tHotelNoticeService.sysUserId
                this.dataForm.title = data.tHotelNoticeService.title
                this.dataForm.content = data.tHotelNoticeService.content
                this.dataForm.creatTime = data.tHotelNoticeService.creatTime
                this.dataForm.reedId = data.tHotelNoticeService.reedId
                this.dataForm.state = data.tHotelNoticeService.state
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelnoticeservice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sysUserId': this.dataForm.sysUserId,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'creatTime': this.dataForm.creatTime,
                'reedId': this.dataForm.reedId,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
